﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlServerCe;

namespace MercuryFE
{
    /// <summary>
    /// Interaction logic for NewAssignment.xaml
    /// </summary>
    public partial class NewAssignment : Window
    {
        //private bool _isMame = false;

        public int assignmentEmulatorID { get; set; }
        public string assignmentSystem { get; set; }

        public NewAssignment()
        {
            InitializeComponent();
            init();
        }


        private void init()
        {
            // get the list of emulators
            Dictionary<int,string> emulatorList = DataLib.getEmulatorList();
            cmbEmulator.ItemsSource = emulatorList;
            cmbEmulator.DisplayMemberPath = "Value";

            cmbSystemMameType.ItemsSource = DataLib.getMameSystemTypes();
            cmbSystemMameType.SelectedIndex = 0;


            cmbSystem.ItemsSource = DataLib.getMameRomList();

            this.assignmentEmulatorID = -1;
            this.assignmentSystem = "";

        }

        private void cmbEmulator_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // check if mame, and show relevant selectors
            int emulatorId = ((KeyValuePair<int, string>)cmbEmulator.SelectedItem).Key;
            if (DataLib.isEmulatorMame(emulatorId))
            {
                //_isMame = true;
                spSystemSelection.Visibility = Visibility.Visible;
            }
            else
            {
                //_isMame = false;
                spSystemSelection.Visibility = Visibility.Hidden;
            }
        }

        private void cmbSystemMameType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string systemType = (string)cmbSystemMameType.SelectedItem;
            if (systemType == "default")
            {
                cmbSystem.ItemsSource = null;
                cmbSystem.Visibility = Visibility.Collapsed;
            }
            else
            {
                if (systemType == "driver")
                    cmbSystem.ItemsSource = DataLib.getMameDriverList();
                else if (systemType == "rom")
                    cmbSystem.ItemsSource = DataLib.getMameRomList();

                cmbSystem.Visibility = Visibility.Visible;
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {

            this.assignmentEmulatorID = ((KeyValuePair<int, string>)cmbEmulator.SelectedItem).Key;
            this.assignmentSystem = (string)cmbSystemMameType.SelectedItem == "default" ? "default" : (string)cmbSystem.SelectedItem;
            this.Close();
        }

        private void cmbCustomGame_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
